#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
using namespace std;

class Solution {
public:
    int maxSubarraySumCircular(vector<int>& nums) {
        int n = nums.size();
        vector<int> f(n + 1), g(n + 1);
        int mx = INT_MIN, mn = INT_MAX, sum = 0;
        for (int i = 1; i <= nums.size(); ++i)
        {
            f[i] = max(f[i - 1] + nums[i - 1], nums[i - 1]);
            g[i] = min(g[i - 1] + nums[i - 1], nums[i - 1]);
            mx = max(f[i], mx);
            mn = min(g[i], mn);
            sum += nums[i - 1];
        }
        return sum == mn ? mx : max(mx, sum - mn);
    }

    int maxProduct(vector<int>& nums) {
        int n = nums.size();
        vector<int> f(n + 1), g(n + 1);
        f[0] = g[0] = 1;
        int mx = INT_MIN;
        for (int i = 1; i <= n; ++i)
        {
            f[i] = max(nums[i - 1], max(f[i - 1] * nums[i - 1], g[i - 1] * nums[i - 1]));
            g[i] = min(nums[i - 1], min(f[i - 1] * nums[i - 1], g[i - 1] * nums[i - 1]));
            mx = max(f[i], mx);
        }
        return mx;
    }
};
